import { createRouter, createWebHashHistory } from "vue-router";
import { getToken } from "../utils/tools";

const routes = [
  {
    path: "/",
    component: () => import("../views/index/index.vue"),
    children: [
      {
        path: "/hospital",
        name: "hospital",
        component: () => import("../views/hospital/hospital.vue"),
      },
      {
        path: "/manager",
        name: "manager",
        component: () => import("../views/manager/index.vue"),
        children: [
          {
            path: "/role",
            name: "role",
            component: () => import("../views/manager/components/role.vue"),
          },
          {
            path: "/manage",
            name: "manage",
            component: () => import("../views/manager/components/manager.vue"),
          },
          {
            path: "/juri",
            name: "juri",
            component: () => import("../views/manager/components/juri.vue"),
          },
        ],
      },
      {
        path: "/indent",
        name: "indent",
        component: () => import("../views/indent/index.vue"),
      },
      {
        path: "/admin",
        name: "admin",
        component: () => import("../views/hospital/admin.vue"),
      },
      {
        path: "/doctor",
        name: "doctor",
        component: () => import("../views/hospital/doctor.vue"),
      },
      // {
      //   path: "/evaluate",
      //   name: "evaluate",
      //   component: () => import("../views/hospital/evaluate.vue"),
      // },
      {
        path: "/drug",
        name: "drug",
        component: () => import("../views/drug/drug.vue"),
      },
      {
        path: "/drugmsg",
        name: "drugmsg",
        component: () => import("../views/drug/drugmsg.vue"),
      },
      // {
      //   path: "/consult",
      //   name: "consult",
      //   component: () => import("../views/hospital/consult.vue"),
      // },
      {
        path: "/article",
        name: "article",
        component: () => import("../views/article/article.vue"),
      },
      {
        path: "/articlemsg",
        name: "articlemsg",
        component: () => import("../views/article/articlemsg.vue"),
      },
      {
        path: "/illnessmsg",
        name: "illnessmsg",
        component: () => import("../views/illness/illnessmsg.vue"),
      },
      {
        path: "/illness",
        name: "illness",
        component: () => import("../views/illness/illness.vue"),
      },
      {
        path: "/user",
        name: "user",
        component: () => import("../views/user/index.vue"),
      },
      {
        path: "/job",
        name: "job",
        component: () => import("../views/hospital/job.vue"),
      },
    ],
  },
  {
    path: "/login",
    name: "login",
    component: () => import("../views/login/login.vue"),
  }
];

const router = createRouter({
  history: createWebHashHistory(),
  routes,
});

// 路由守卫
router.beforeEach((to, from, next) => { // 前置守卫(guard)
  if (from.path == '/') {
    if (!getToken() && to.name !== 'login') {
      console.log(from.path, '1');
      next({ name: 'login' })
    } else {
      console.log(from.path, '23');
      next()
    }
  } else {
    next()
  }
})

export default router;
